package com.sxbdjw.situation.bdjw;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RestApi {

    private final String TAG = "RestApi";

    //    private String url = "https://api.sxmbxt.com/communication/rtc/";
    private String url = Config.REST_URL;

    /**
     * 发送加入房间
     */
    public void sendInRoom(String userId, String roomId, String userName, String type) {
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).build();

        String reqUrl = url + "inRoom?userId=" + userId + "&roomId=" + roomId + "&userName=" + userName + "&type=" + type + "&time=" + new Date().getTime();

        Request request = new Request.Builder().url(reqUrl).get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e(TAG, "inRoom 请求异常");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Log.e(TAG, reqUrl + "请求" + String.valueOf(response.code()));
            }
        });
    }

    public void outRoom(String userId, String roomId, String userName) {
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).build();

        String reqUrl = url + "exitRoom?userId=" + userId + "&roomId=" + roomId + "&userName=" + userName + "&time=" + new Date().getTime();
        System.out.println(reqUrl);
        Request request = new Request.Builder().url(reqUrl).get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e(TAG, "exitRoom 请求异常");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Log.e(TAG, String.valueOf(response.code()));
            }
        });
    }


    public void refuse(String userId, String roomId, String userName) {
        System.out.println("refuse");
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).build();
        String reqUrl = url + "refuse?userId=" + userId + "&roomId=" + roomId + "&userName=" + userName + "&time=" + new Date().getTime();
        Request request = new Request.Builder().url(reqUrl).get().build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e(TAG, "refuse 请求异常");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Log.e(TAG, String.valueOf(response.code()));
            }
        });
    }
}
